package com.taikang.hmp.hservice.doctor.healthrickevalmodel.assess;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.bean.HealthTag;
import com.taikang.hmp.hservice.doctor.healthrickevalmodel.bean.HypertensionReport;
import com.taikang.hmp.hservice.doctor.healthrickevalmodel.bean.KnowledgeBase;
import com.taikang.hmp.hservice.doctor.healthrickevalmodel.service.IAssessService;

public class AssessAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(AssessAction.class);

	private IAssessService assessService;

	public void setAssessService(IAssessService assessService) {
		this.assessService = assessService;
	}

	public String executeAction() {
		if ("metabolicSyndrome".equals(requestDto.getMethod())) {
			return metabolicSyndrome();
		} else if ("submitdxzhz".equals(requestDto.getMethod())) {
			return submitdxzhz();
		} else if ("submittnb".equals(requestDto.getMethod())) {
			return submittnb();
		} else if ("dMReport".equals(requestDto.getMethod())) {
			return dMReport();
		} else if ("cHDReport".equals(requestDto.getMethod())) {
			return cHDReport();
		} else if ("submitgxy".equals(requestDto.getMethod())) {
			return submitgxy();
		} else if ("hypertensionReport".equals(requestDto.getMethod())) {
			return hypertensionReport();
		} else if ("submitxxg".equals(requestDto.getMethod())) {
			return submitxxg();
		} else if ("submitResulthealthRisks".equals(requestDto.getMethod())) {
			return submitResulthealthRisks();
		} else if ("resulthealthRisks".equals(requestDto.getMethod())) {
			return resulthealthRisks();
		} else {
			throw new TKException("no support method:" + requestDto.getMethod());
		}
	}

	// // 评估报告
	// private HypertensionReport hypertensionReport;
	//
	// public HypertensionReport getHypertensionReport() {
	// return hypertensionReport;
	// }

	public String submitdxzhz() {

		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = new HypertensionReport();
		domain.setCustomerid(paramDto.getAsString("customerid"));
		if (paramDto.getAsString("1").indexOf("@") != -1) {
			domain.setHypertension(0);
			domain.setDm(0);

			String[] str = paramDto.getAsString("1").split("@");
			for (String a : str) {
				if (a.equals("A")) {
					domain.setHypertension(1);
				} else if (a.equals("B")) {
					domain.setDm(1);
				}
			}
		} else {
			domain.setHypertension(0);
			domain.setDm(0);
			String a = paramDto.getAsString("1");
			if (a.equals("A")) {
				domain.setHypertension(1);
			} else if (a.equals("B")) {
				domain.setDm(1);
			}
		}

		domain.setHeight(paramDto.getAsString("height"));
		domain.setWeight(paramDto.getAsString("weight"));
		domain.setSex(paramDto.getAsString("sex"));
		try {
			// 得到生日
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			String ss = paramDto.getAsString("birthday");
			Date birthday = f.parse(ss);
			Date sysdate = new Date();
			int age = sysdate.getYear() - birthday.getYear();
			domain.setAge(age);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		domain.setSystolic(paramDto.getAsString("3")); // 收缩压
		domain.setDiastolic(paramDto.getAsString("4"));// 舒张压
		domain.setHdl(paramDto.getAsString("7"));// 高密度蛋白
		domain.setTg(paramDto.getAsString("6"));
		domain.setFastingbg(paramDto.getAsString("9"));
		domain.setPostprandialbg(paramDto.getAsString("10"));
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = ff.format(new Date());
		domain.setSavedate(date);
		domain.setEvalnumberid("1");
		domain.setId(java.util.UUID.randomUUID().toString());
		String url = assessService.submitdxzhz(domain);

		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("url", url);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	// 代谢综合征
	public String metabolicSyndrome() {

		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = assessService.querybc(paramDto);
		KnowledgeBase knowledgeBase = new KnowledgeBase();
		knowledgeBase.setValuationType("代谢综合征");
		double bmi = Math.round(Double.parseDouble(domain.getWeight())
				/ Math.pow(Double.parseDouble(domain.getHeight()) / 100, 2)
				* 10) / 10.0;
		domain.setBmi(bmi);
		String risks = "";
		int flag = 0;
		if (bmi >= 25) {
			// 肥胖
			flag += 1;
		}
		int userself = domain.getDm();
		// 空腹血糖
		double fastingBg = Double.parseDouble(domain.getFastingbg());
		// 餐后两小时血糖
		double postprandialBg = 0.0;
		if (domain.getPostprandialbg() != null) {
			postprandialBg = Double.parseDouble(domain.getPostprandialbg());
		}
		boolean xueTang = fastingBg >= 6.1 || userself > 0;
		if (postprandialBg != 0.0) {
			xueTang = fastingBg >= 6.1 || userself > 0 || postprandialBg >= 7.8;
		}
		// xuetang
		if (xueTang) {
			flag += 1;
		}
		// 收缩压
		int systolic = Integer.valueOf(domain.getSystolic());
		// 舒张压
		int diastolic = Integer.valueOf(domain.getDiastolic());
		int sex = 0;
		if (domain.getSex() != null) {
			sex = Integer.parseInt(domain.getSex());
		}
		int userself2 = domain.getHypertension();
		// 血压
		if (systolic >= 140 || diastolic >= 90 || userself2 > 0) {
			flag += 1;
		}
		// 甘油三酯
		double tg = 0;
		if (domain.getTg() != null)
			tg = Double.parseDouble(domain.getTg());
		if (tg >= 1.7) {
			flag += 1;
		}
		// 高密度脂肪蛋白
		double hdl = 0;
		if (domain.getHdl() != null)
			hdl = Double.parseDouble(domain.getHdl());
		if (hdl != 0) { // 未测量时无效
			if (sex == 0) {
				if (hdl < 0.9) {
					flag += 1;
				}
			} else {
				if (hdl < 1.0) {
					flag += 1;
				}
			}
		}
		if (bmi >= 24 || bmi < 18.5) {
			risks += "体重、";
		}
		if (systolic >= 120 || diastolic >= 80) {
			risks += "血压、";
		}
		if ("".equals(risks)) {
			risks += "无";
		} else {
			risks = risks.substring(0, risks.length() - 1);
		}

		if (flag == 0) {
			knowledgeBase.setRiskGrade("低危人群");
		} else if (flag >= 1 && flag < 3) {
			knowledgeBase.setRiskGrade("高危人群");
		} else if (flag >= 3) {
			knowledgeBase.setRiskGrade("代谢综合征");
		}
		domain.setRisks(risks);
		String str1 = "1、代谢综合征是指在个体中多种代谢异常情况集结存在的现象。"
				+ "这些异常包括糖尿病调节受损、高血压、血脂紊乱、全身或腹部肥胖等。"
				+ "这些代谢异常大多为动脉硬化性心、脑及周围血管病（简称心血管疾病）的危险因素，"
				+ "故代谢综合征患者是心血管疾病的高危人群；";
		String str2 = "2、根据您目前健康状况，您属于代谢综合征的";
		String str3 = "3、您可以改善的危险因素有：";
		String str4 = "4、为了您的健康，降低代谢相关疾病的发生风险，请从现在开始改变生活方式。";

		knowledgeBase.setKnowledgeBase1(str1);
		knowledgeBase.setKnowledgeBase2(str2);
		knowledgeBase.setKnowledgeBase3(str3);
		knowledgeBase.setKnowledgeBase4(str4);
		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("hypertensionReport", domain);
		resultDto.put("knowledgeBaseResult", knowledgeBase);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	public String submittnb() {
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = new HypertensionReport();
		domain.setCustomerid(paramDto.getAsString("customerid"));

		domain.setHypertension(0);
		domain.setDm(0);
		domain.setChd(0);
		if (paramDto.getAsString("1").indexOf("@") != -1) {
			String[] str = paramDto.getAsString("1").split("@");
			for (String a : str) {
				if (a.equals("A")) {
					domain.setHypertension(1);
				} else if (a.equals("B")) {
					domain.setDm(1);
				} else if (a.equals("C")) {
					domain.setChd(1);
				}
			}
		} else {
			String a = paramDto.getAsString("1");
			if (a.equals("A")) {
				domain.setHypertension(1);
			} else if (a.equals("B")) {
				domain.setDm(1);
			} else if (a.equals("C")) {
				domain.setChd(1);
			}
		}
		domain.setHeight(paramDto.getAsString("height"));
		domain.setWeight(paramDto.getAsString("weight"));
		domain.setWaist(paramDto.getAsString("waist"));
		domain.setHip(paramDto.getAsString("hip"));
		domain.setUlcer("0");
		domain.setPolyuria("0");
		if (paramDto.getAsString("5").indexOf("@") != -1) {
			String[] str = paramDto.getAsString("5").split("@");
			for (String a : str) {
				if (a.equals("3")) {
					domain.setUlcer("1");
				} else if (a.equals("7")) {
					domain.setPolyuria("1");
				}
			}
		} else {
			String a = paramDto.getAsString("5");
			if (a.equals("3")) {
				domain.setUlcer("1");
			} else if (a.equals("7")) {
				domain.setPolyuria("1");
			}
		}

		domain.setSex(paramDto.getAsString("sex")); // 性别
		try {
			// 得到生日
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			String ss = paramDto.getAsString("birthday");
			Date birthday = f.parse(ss);
			Date sysdate = new Date();
			int age = sysdate.getYear() - birthday.getYear();
			domain.setAge(age);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		domain.setEducation(paramDto.getAsString("2")); // 学历
		domain.setFdisease(Integer.valueOf(paramDto.getAsString("3")));// 是否有病

		domain.setSystolic(paramDto.getAsString("7")); // 收缩压
		domain.setDiastolic(paramDto.getAsString("8"));// 舒张压
		domain.setPulse(paramDto.getAsString("9"));
		domain.setFastingbg(paramDto.getAsString("10"));
		domain.setPostprandialbg(paramDto.getAsString("11"));
		domain.setGlycosylatedhemoglobin(paramDto.getAsString("12"));
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = ff.format(new Date());
		domain.setSavedate(date);
		domain.setEvalnumberid("2");
		domain.setId(java.util.UUID.randomUUID().toString());
		String url = assessService.submittnb(domain);

		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("url", url);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	// 糖尿病评估报告
	public String dMReport() {

		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = assessService.querybc(paramDto);

		KnowledgeBase knowledgeBase = new KnowledgeBase();
		knowledgeBase.setValuationType("糖尿病");

		int userself = domain.getDm();// 糖尿病
		// 用户是否得病
		domain.setIshaddisease(userself);
		// 生日
		int age = domain.getAge();

		double whr = 0;
		if (domain.getWaist() != null && domain.getHip() != null) {
			double waist = Double.parseDouble(domain.getWaist());
			double hip = Double.parseDouble(domain.getHip());
			if (hip != 0) {
				whr = Math.round(waist / hip * 100.0) / 100.0;
				domain.setWhr(String.valueOf(whr));
			}
		}

		// 空腹血糖
		double fastingBg = 0.0;
		if (domain.getFastingbg() != null) {
			fastingBg = Double.parseDouble(domain.getFastingbg());
		}
		// 餐后两小时血糖
		double postprandialBg = 0.0;
		if (domain.getPostprandialbg() != null) {
			postprandialBg = Double.parseDouble(domain.getPostprandialbg());
		}
		// 糖化血红蛋白
		double glycosylatedHemoglobin = 0.0;
		if (domain.getGlycosylatedhemoglobin() != null) {
			glycosylatedHemoglobin = Double.parseDouble(domain
					.getGlycosylatedhemoglobin());
		}
		int fdisease = domain.getFdisease();
		// 身高体重比
		double bmi = Math.round(Double.parseDouble(domain.getWeight())
				/ Math.pow(Double.parseDouble(domain.getHeight()) / 100, 2)
				* 10) / 10.0;
		domain.setBmi(bmi);
		// 收缩压
		int systolic = Integer.valueOf(domain.getSystolic());
		// 舒张压
		int diastolic = Integer.valueOf(domain.getDiastolic());
		int sex = 0;
		if (domain.getSex() != null)
			sex = Integer.parseInt(domain.getSex());
		// 多饮多尿
		int polyuria = 0;
		if (domain.getPolyuria() != null)
			polyuria = Integer.parseInt(domain.getPolyuria());
		// 肢端溃疡
		int ulcer = 0;
		if (domain.getUlcer() != null)
			ulcer = Integer.parseInt(domain.getUlcer());
		// 脉搏
		int pulse = 0;
		if (domain.getPulse() != null) {
			if (Integer.valueOf(domain.getPulse()) > 90) {
				pulse = 1;
			}
		}
		// 学历
		int education = 0;
		if (domain.getEducation() != null)
			education = Integer.parseInt(domain.getEducation());
		String hypertensionlevel = "";
		String risks = "";
		if (pulse > 0) {
			risks += "脉搏、";
		}
		if (bmi >= 24 || bmi < 18.5) {
			risks += "体重、";
		}
		if (systolic >= 140 || diastolic >= 90) {
			risks += "血压、";
		}
		if ((sex == 0 && whr >= 0.9) || (sex == 1 && whr >= 0.85)) {
			risks += "腰臀围比、";
		}
		if ("".equals(risks)) {
			risks += "无";
		} else {
			risks = risks.substring(0, risks.length() - 1);
		}
		domain.setRisks(risks);

		boolean fengXian = !(userself > 0) && fastingBg < 7.0;
		if (postprandialBg != 0.0) {
			fengXian = !(userself > 0) && fastingBg < 7.0
					&& postprandialBg < 11.1;
		}
		boolean shouSun = !(userself > 0)
				&& (6.1 <= fastingBg && fastingBg < 7.0);
		// 6.1<=fastingBg<7.0 || 7.8<=postprandialBg<11.1
		// 根据您目前的健康状况，您疑似糖尿病前期糖调节受损。建议您及早到正规医院进行确诊，早诊断早治疗。
		if (postprandialBg != 0.0) {
			shouSun = !(userself > 0)
					&& ((6.1 <= fastingBg && fastingBg < 7.0) || (7.8 <= postprandialBg && postprandialBg < 11.1));
		}
		boolean yiSi = !(userself > 0) && (fastingBg >= 7.0);
		if (postprandialBg != 0.0) {
			yiSi = !(userself > 0)
					&& (fastingBg >= 7.0 || postprandialBg >= 11.1);
		}// glycosylatedHemoglobin糖化大于6.0%
//		if (glycosylatedHemoglobin != 0.0) {
//			yiSi = !(userself > 0)
//					&& (fastingBg >= 7.0 || postprandialBg >= 11.1 || glycosylatedHemoglobin > 6.0);
//		}
		if (userself > 0) {
			domain.setPagecontent("0");
			domain.setHypertensionlevel("已患糖尿病。");
			knowledgeBase.setRiskGrade("已患");
		} else if (yiSi) {
			domain.setPagecontent("1");
			knowledgeBase.setRiskGrade("疑似");
			domain.setHypertensionlevel("根据您目前的健康状况，您为疑似糖尿病。建议您及早到正规医院进行确诊，早诊断早治疗。");
		} else if (shouSun) {
			domain.setPagecontent("3");
			knowledgeBase.setRiskGrade("糖调节受损");
			domain.setHypertensionlevel("根据您目前的健康状况，您已达到糖调节受损，建议您及早到正规医院做进一步检查，早诊断早治疗。");
		} else if (fengXian) {
			// 糖尿病风险评估
			domain.setPagecontent("2");
			knowledgeBase.setRiskGrade("未患");
			int score = 0;
			if (age >= 40 && age <= 49) {
				score += 2;
			} else if (age >= 50 && age <= 59) {
				score += 4;
			} else if (age >= 60) {
				score += 6;
			}
			if (education > 0) {
				score += 2;
			}
			if (polyuria > 0) {
				score += 2;
			}
			if (ulcer > 0) {
				score += 2;
			}
			if (domain.getHypertension() > 0) {
				score += 3;
			}
			if (domain.getChd() > 0) {
				score += 2;
			}
			if (domain.getFdisease() > 0) {
				score += 2;
			}
			int standardScore = score;
			if (bmi >= 25.0 && bmi < 27.0) {
				score += 2;
			} else if (bmi >= 27.0) {
				score += 4;
			}
			if (sex == 0) {
				if (whr >= 0.90 && whr <= 0.94) {
					score += 2;
				} else if (whr >= 0.95) {
					score += 4;
				}
			} else {
				if (whr >= 0.85 && whr <= 0.89) {
					score += 2;
				} else if (whr >= 0.90) {
					score += 4;
				}
			}
			if (systolic >= 140 && systolic <= 159) {
				score += 3;
			} else if (systolic >= 160) {
				score += 6;
			}
			if (diastolic >= 90 && diastolic <= 94) {
				score += 2;
			} else if (diastolic >= 95) {
				score += 4;
			}
			if (pulse > 0) {
				score += 2;
			}

			domain.setAttack(score);
			domain.setStandardattack(standardScore);
			// 判断分数
			domain.setHypertensionlevel(this.getfengxian(score));
			domain.setStandardlevel(this.getfengxian(standardScore));
		}
		String str1;
		if (userself > 0) {
			str1 = "1、根据您目前健康状况，您";
		} else {
			str1 = "1、根据您目前健康状况，您糖尿病的发病风险为";
		}
		String str2 = "";
		if (userself > 0 || yiSi || shouSun) {
			str2 = "2、高风险因素会导致未来糖尿病并发症的风险增大；";
		} else if (fengXian) {
			str2 = "2、只要将可以改变的危险因素降至参考值范围内，您的糖尿病发病风险将降为";
		}
		String str3 = "3、您可以改善的危险因素有：";
		String str4 = "4、为了您的健康，请从现在开始改变生活方式，以降低疾病的发病风险。";

		knowledgeBase.setKnowledgeBase1(str1);
		knowledgeBase.setKnowledgeBase2(str2);
		knowledgeBase.setKnowledgeBase3(str3);
		knowledgeBase.setKnowledgeBase4(str4);
		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("hypertensionReport", domain);
		resultDto.put("knowledgeBaseResult", knowledgeBase);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	public String submitgxy() {
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = new HypertensionReport();
		domain.setCustomerid(paramDto.getAsString("customerid"));
		domain.setHypertension(0);
		domain.setDm(0);
		domain.setChd(0);
		if (paramDto.getAsString("1").indexOf("@") != -1) {
			domain.setHypertension(0);
			domain.setDm(0);
			domain.setChd(0);
			String[] str = paramDto.getAsString("1").split("@");
			for (String a : str) {
				if (a.equals("A")) {
					domain.setHypertension(1);
				} else if (a.equals("B")) {
					domain.setDm(1);
				} else if (a.equals("C")) {
					domain.setChd(1);
				}
			}
		} else {
			String a = paramDto.getAsString("1");
			domain.setHypertension(0);
			domain.setDm(0);
			domain.setChd(0);
			if (a.equals("A")) {
				domain.setHypertension(1);
			} else if (a.equals("B")) {
				domain.setDm(1);
			} else if (a.equals("C")) {
				domain.setChd(1);
			}
		}

		domain.setHeight(paramDto.getAsString("height"));
		domain.setWeight(paramDto.getAsString("weight"));

		domain.setSystolic(paramDto.getAsString("5")); // 收缩压
		domain.setDiastolic(paramDto.getAsString("6"));// 舒张压

		domain.setSmoke(paramDto.getAsString("3"));
		domain.setFdisease(Integer.valueOf(paramDto.getAsString("2")));

		domain.setSex(paramDto.getAsString("sex")); // 性别

		try {
			// 得到生日
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			String ss = paramDto.getAsString("birthday");
			Date birthday = f.parse(ss);
			Date sysdate = new Date();
			int age = sysdate.getYear() - birthday.getYear();
			domain.setAge(age);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = ff.format(new Date());
		domain.setSavedate(date);
		domain.setEvalnumberid("3");
		domain.setId(java.util.UUID.randomUUID().toString());
		String url = assessService.submitgxy(domain);

		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("url", url);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	// // 高血压评估
	public String hypertensionReport() {
		KnowledgeBase knowledgeBase = new KnowledgeBase();
		knowledgeBase.setValuationType("高血压");

		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = assessService.querybc(paramDto);

		HypertensionReport hypertensionReportTwo = null;
		HypertensionReport hypertensionReportOne = null;
		int userself = domain.getHypertension();
		// 生日
		int age = domain.getAge();
		// 用户是否得病
		domain.setIshaddisease(userself);

		int fdisease = domain.getFdisease();
		double bmi = Math.round(Double.parseDouble(domain.getWeight())
				/ Math.pow(Double.parseDouble(domain.getHeight()) / 100, 2)
				* 10) / 10.0;
		domain.setBmi(bmi);
		int smoke = 0;
		if (domain.getSmoke() != null)
			smoke = Integer.parseInt(domain.getSmoke());
		int systolic = Integer.valueOf(domain.getSystolic());
		int diastolic = Integer.valueOf(domain.getDiastolic());
		int sex = 0;
		if (domain.getSex() != null)
			sex = Integer.parseInt(domain.getSex());
		String hypertensionLevel = "";
		String risks = "";
		if (bmi > 23.9 || bmi < 18.5) {
			risks += "体重、";
		}
		if (systolic >= 120 || diastolic >= 80) {
			risks += "血压、";
		}
		if (smoke == 0) {
			risks += "吸烟、";
		}
		if ("".equals(risks)) {
			risks += "无";
		} else {
			risks = risks.substring(0, risks.length() - 1);
		}
		domain.setRisks(risks);
		if (userself > 0) {
			domain.setPagecontent("0");
			knowledgeBase.setRiskGrade("已患");
			domain.setHypertensionlevel("已患高血压。");
		} else if (!(userself > 0) && (systolic >= 140 || diastolic >= 90)) {
			domain.setPagecontent("1");
			knowledgeBase.setRiskGrade("疑似");
			if (systolic >= 180 || diastolic >= 110) {
				domain.setHypertensionlevel("根据您目前的健康指标，您的血压已达到三级高血压的水平，建议您立刻正规医院进行确诊，以免发生危险。");
			} else if ((systolic >= 160 && systolic < 180)
					|| (diastolic >= 100 && diastolic < 110)) {
				domain.setHypertensionlevel("根据您目前的健康指标，您的血压已达到二级高血压的水平，建议您及早到正规医院进行确诊，早诊断早治疗。");
			} else if ((systolic >= 140 && systolic < 160)
					|| (diastolic >= 90 && diastolic < 100)) {
				domain.setHypertensionlevel("根据您目前的健康指标，您的血压已达到一级高血压的水平，建议您及早到正规医院进行确诊，早诊断早治疗。");
			}
		} else if (!(userself > 0) && systolic < 140 && diastolic < 90) {
			// 没有高血压风险评估
			knowledgeBase.setRiskGrade("未患");
			domain.setPagecontent("2");

			int _smoke = smoke == 1 ? 0 : 1;
			int _fdisease = fdisease == 2 ? 0 : (fdisease + 1);
			int _systolic = systolic < 120 ? 100 : systolic;
			int _diastolic = diastolic < 80 ? 76 : diastolic;
			double beta = 22.949536 + (-0.156412 * age) + (-0.202933 * sex)
					+ (-0.033881 * bmi) + (-0.05933 * _systolic)
					+ (-0.128468 * _diastolic) + (-0.190731 * _smoke)
					+ (-0.166121 * _fdisease) + (0.001624 * age * _diastolic);
			double fourAttack = 1 - (Math
					.exp(-Math.exp((Math.log(4) - beta) / 0.876925)));
			double twoAttack = 1 - (Math
					.exp(-Math.exp((Math.log(2) - beta) / 0.876925)));
			double oneAttack = 1 - (Math
					.exp(-Math.exp((Math.log(1) - beta) / 0.876925)));
			domain.setAttack(fourAttack);
			double fourStandardAttack = this.getStangdardAcctack(domain, 4);
			domain.setStandardattack(fourStandardAttack);

			hypertensionReportTwo = new HypertensionReport();
			hypertensionReportTwo.setBmi(domain.getBmi());
			hypertensionReportTwo.setSystolic(domain.getSystolic());
			hypertensionReportTwo.setDiastolic(domain.getDiastolic());
			hypertensionReportTwo.setSex(domain.getSex());
			hypertensionReportTwo.setAge(domain.getAge());
			hypertensionReportTwo.setFdisease(domain.getFdisease());
			hypertensionReportTwo.setAttack(twoAttack);
			double twoStandardAttack = this.getStangdardAcctack(
					hypertensionReportTwo, 2);
			hypertensionReportTwo.setStandardattack(twoStandardAttack);

			hypertensionReportOne = new HypertensionReport();
			hypertensionReportOne.setBmi(domain.getBmi());
			hypertensionReportOne.setSystolic(domain.getSystolic());
			hypertensionReportOne.setDiastolic(domain.getDiastolic());
			hypertensionReportOne.setSex(domain.getSex());
			hypertensionReportOne.setAge(domain.getAge());
			hypertensionReportOne.setFdisease(domain.getFdisease());
			hypertensionReportOne.setAttack(oneAttack);
			double oneStandardAttack = this.getStangdardAcctack(
					hypertensionReportOne, 1);
			hypertensionReportOne.setStandardattack(oneStandardAttack);
		}

		String str1 = "", str2 = "", str3 = "", str4 = "";
		if (knowledgeBase.getRiskGrade().equals("未患")) {

			str1 = "1、根据您目前健康状况，未来1、2、4年高血压的发病风险分别为";
			str2 = "2、只要将可以改变的危险因素降至参考值范围内，您的糖尿病发病风险将降为";
			str3 = "3、您可以改善的危险因素有：";
			str4 = "4、为了您的健康，请从现在开始改变生活方式，以降低疾病的发病风险。";
		} else if (knowledgeBase.getRiskGrade().equals("已患")) {
			str1 = "1、根据您目前健康状况，您已患高血压；";
			str2 = "2、高风险因素会导致高血压并发症的风险增大；";
			str3 = "3、您可以改善的危险因素有：";
			str4 = "4、为了您的健康，降低并发症的发生，提高生活质量，请从现在开始改变生活方式。";

		} else if (knowledgeBase.getRiskGrade().equals("疑似")) {
			str1 = "1、根据您目前健康状况，您疑似高血压；";
			str2 = "2、高风险因素会导致未来高血压及并发症的风险增大；";
			str3 = "3、您可以改善的危险因素有：";
			str4 = "4、为了您的健康，提高生活质量，请从现在开始改变生活方式，以降低疾病及其并发症的发生风险。";

		}
		knowledgeBase.setKnowledgeBase1(str1);
		knowledgeBase.setKnowledgeBase2(str2);
		knowledgeBase.setKnowledgeBase3(str3);
		knowledgeBase.setKnowledgeBase4(str4);
		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();

		resultDto.put("knowledgeBaseResult", knowledgeBase);
		resultDto.put("hypertensionReport", domain);
		resultDto.put("hypertensionReportTwo", hypertensionReportTwo);
		resultDto.put("hypertensionReportOne", hypertensionReportOne);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	public String submitxxg() {
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = new HypertensionReport();
		domain.setCustomerid(paramDto.getAsString("customerid"));

		domain.setHypertension(0);
		domain.setDm(0);
		domain.setChd(0);
		if (paramDto.getAsString("1").indexOf("@") != -1) {
			domain.setHypertension(0);
			domain.setDm(0);
			domain.setChd(0);
			domain.setStroke(0);
			String[] str = paramDto.getAsString("1").split("@");
			for (String a : str) {
				if (a.equals("A")) {
					domain.setHypertension(1);
				} else if (a.equals("B")) {
					domain.setDm(1);
				} else if (a.equals("C")) {
					domain.setChd(1);
				} else if (a.equals("D")) {
					domain.setStroke(1);
				}
			}
		} else {
			String a = paramDto.getAsString("1");
			domain.setHypertension(0);
			domain.setDm(0);
			domain.setChd(0);
			domain.setStroke(0);
			if (a.equals("A")) {
				domain.setHypertension(1);
			} else if (a.equals("B")) {
				domain.setDm(1);
			} else if (a.equals("C")) {
				domain.setChd(1);
			} else if (a.equals("D")) {
				domain.setStroke(1);
			}
		}

		domain.setHeight(paramDto.getAsString("height"));
		domain.setWeight(paramDto.getAsString("weight"));

		domain.setSystolic(paramDto.getAsString("4")); // 收缩压
		domain.setDiastolic(paramDto.getAsString("5"));// 舒张压

		domain.setTc(paramDto.getAsString("6"));// 总胆固醇

		domain.setSmoke(paramDto.getAsString("3"));

		domain.setSex(paramDto.getAsString("sex")); // 性别

		try {
			// 得到生日
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			String ss = paramDto.getAsString("birthday");
			Date birthday = f.parse(ss);
			Date sysdate = new Date();
			int age = sysdate.getYear() - birthday.getYear();
			domain.setAge(age);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = ff.format(new Date());
		domain.setSavedate(date);
		domain.setEvalnumberid("4");
		domain.setId(java.util.UUID.randomUUID().toString());
		String url = assessService.submitxxg(domain);

		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("url", url);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	/**
	 * 缺血性心血管疾病报告
	 * 
	 * @return
	 */
	public String cHDReport() {
		KnowledgeBase knowledgeBase = new KnowledgeBase();
		knowledgeBase.setValuationType("缺血性心血管");

		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = assessService.querybc(paramDto);
		// 判断是否已患病
		int userself = domain.getChd();
		int userself2 = domain.getStroke();
		if (userself > 0 || userself2 > 0) {
			domain.setIshaddisease(1);
			knowledgeBase.setRiskGrade("已患");
		} else {
			domain.setIshaddisease(0);
			knowledgeBase.setRiskGrade("未患");
		}
		// 生日
		int age = domain.getAge();
		double bmi = Math.round(Double.parseDouble(domain.getWeight())
				/ Math.pow(Double.parseDouble(domain.getHeight()) / 100, 2)
				* 10) / 10.0;
		domain.setBmi(bmi);
		int smoke = 0;
		if (domain.getSmoke() != null)
			smoke = Integer.parseInt(domain.getSmoke());
		int systolic = Integer.valueOf(domain.getSystolic());
		int diastolic = Integer.valueOf(domain.getDiastolic());
		int sex = 0;
		if (domain.getSex() != null)
			sex = Integer.parseInt(domain.getSex());
		int dm = domain.getDm();
		double tc = 0;
		if (domain.getTc() != null)
			tc = Double.parseDouble(domain.getTc());
		// 危险因素
		String risks = "";
		if (bmi > 23.9 || bmi < 18.5) {
			risks += "体重、";
		}
		if (systolic >= 120 || diastolic >= 80) {
			risks += "血压、";
		}
		if (smoke == 0) {
			risks += "吸烟、";
		}
		if (tc > 5.18) {
			risks += "总胆固醇、";
		}
		// if (hdl < 1.04) {
		// risks += "高密度脂蛋白胆固醇、";
		// }
		if ("".equals(risks)) {
			risks = "无";
		} else {
			risks = risks.substring(0, risks.length() - 1);
		}
		domain.setRisks(risks);
		int score = 0;

		// 年龄
		if (age <= 39) {
			score += 0;
		} else if (age >= 40 && age <= 44) {
			score += 1;
		} else if (age >= 45 && age <= 49) {
			score += 2;
		} else if (age >= 50 && age <= 54) {
			score += 3;
		} else if (age >= 55 && age <= 59) {
			score += 4;
		} else if (age >= 60 && age <= 64) {
			score += 5;
		} else if (age >= 65 && age <= 79) {
			score += 6;
		} else if (age >= 80 && age <= 84) {
			score += 7;
		} else if (age >= 85) {
			score += 8;
		}
		// 体重指数
		if (bmi < 24) {
			score += 0;
		} else if (bmi >= 24 && bmi < 28) {
			score += 1;
		} else if (bmi >= 28) {
			score += 2;
		}
		// 胆固醇
		if (tc < 5.2) {
			score += 0;
		} else if (tc >= 5.2) {
			score += 1;
		}

		if (sex == 0) {
			// 吸烟
			if (smoke == 0) {
				score += 2;
			} else {
				score += 0;
			}
			// 糖尿病
			if (dm == 1) {
				score += 1;
			} else {
				score += 0;
			}
			// 收缩压
			if (systolic == 0) {
				score += 0;
			} else if (systolic < 120) {
				score += -2;
			} else if (systolic >= 120 && systolic < 130) {
				score += 0;
			} else if (systolic >= 130 && systolic < 140) {
				score += 1;
			} else if (systolic >= 140 && systolic < 160) {
				score += 2;
			} else if (systolic >= 160 && systolic < 180) {
				score += 5;
			} else if (systolic >= 180) {
				score += 8;
			}
			if (score == -2) {
				domain.setAttack(0.3);
			} else if (score == -1) {
				domain.setAttack(0.3);
			} else if (score == 0) {
				domain.setAttack(0.5);
			} else if (score == 1) {
				domain.setAttack(0.6);
			} else if (score == 2) {
				domain.setAttack(0.8);
			} else if (score == 3) {
				domain.setAttack(1.1);
			} else if (score == 4) {
				domain.setAttack(1.5);
			} else if (score == 5) {
				domain.setAttack(2.1);
			} else if (score == 6) {
				domain.setAttack(2.9);
			} else if (score == 7) {
				domain.setAttack(3.9);
			} else if (score == 8) {
				domain.setAttack(5.4);
			} else if (score == 9) {
				domain.setAttack(7.3);
			} else if (score == 10) {
				domain.setAttack(9.7);
			} else if (score == 11) {
				domain.setAttack(12.8);
			} else if (score == 12) {
				domain.setAttack(16.8);
			} else if (score == 13) {
				domain.setAttack(21.7);
			} else if (score == 14) {
				domain.setAttack(27.7);
			} else if (score == 15) {
				domain.setAttack(35.3);
			} else if (score == 16) {
				domain.setAttack(44.3);
			} else if (score >= 17) {
				domain.setAttack(52.6);
			}
			int standardscore = 0;
			if (age <= 39) {
				standardscore += 0;
			} else if (age >= 40 && age <= 44) {
				standardscore += 1;
			} else if (age >= 45 && age <= 49) {
				standardscore += 2;
			} else if (age >= 50 && age <= 54) {
				standardscore += 3;
			} else if (age >= 55 && age <= 59) {
				standardscore += 4;
			} else if (age >= 60 && age <= 64) {
				standardscore += 5;
			} else if (age >= 65 && age <= 79) {
				standardscore += 6;
			} else if (age >= 80 && age <= 84) {
				standardscore += 7;
			} else if (age >= 85) {
				standardscore += 8;
			}
			// 糖尿病
			if (dm == 1) {
				standardscore += 1;
			} else {
				standardscore += 0;
			}
			standardscore = standardscore - 2;
			if (standardscore == -2) {
				domain.setStandardattack(0.3);
			} else if (standardscore == -1) {
				domain.setStandardattack(0.3);
			} else if (standardscore == 0) {
				domain.setStandardattack(0.5);
			} else if (standardscore == 1) {
				domain.setStandardattack(0.6);
			} else if (standardscore == 2) {
				domain.setStandardattack(0.8);
			} else if (standardscore == 3) {
				domain.setStandardattack(1.1);
			} else if (standardscore == 4) {
				domain.setStandardattack(1.5);
			} else if (standardscore == 5) {
				domain.setStandardattack(2.1);
			} else if (standardscore == 6) {
				domain.setStandardattack(2.9);
			} else if (standardscore == 7) {
				domain.setStandardattack(3.9);
			}
		} else {
			// 女性
			// 吸烟
			if (smoke == 0) {
				score += 1;
			} else {
				score += 0;
			}
			// 糖尿病
			if (dm == 1) {
				score += 2;
			} else {
				score += 0;
			}
			// 收缩压
			if (systolic == 0) {
				score += 0;
			} else if (systolic < 120) {
				score += -2;
			} else if (systolic >= 120 && systolic < 130) {
				score += 0;
			} else if (systolic >= 130 && systolic < 140) {
				score += 1;
			} else if (systolic >= 140 && systolic < 160) {
				score += 2;
			} else if (systolic >= 160 && systolic < 180) {
				score += 3;
			} else if (systolic >= 180) {
				score += 4;
			}
			if (score == -2) {
				domain.setAttack(0.1);
			} else if (score == -1) {
				domain.setAttack(0.2);
			} else if (score == 0) {
				domain.setAttack(0.2);
			} else if (score == 1) {
				domain.setAttack(0.3);
			} else if (score == 2) {
				domain.setAttack(0.5);
			} else if (score == 3) {
				domain.setAttack(0.8);
			} else if (score == 4) {
				domain.setAttack(1.2);
			} else if (score == 5) {
				domain.setAttack(1.8);
			} else if (score == 6) {
				domain.setAttack(2.8);
			} else if (score == 7) {
				domain.setAttack(4.4);
			} else if (score == 8) {
				domain.setAttack(6.8);
			} else if (score == 9) {
				domain.setAttack(10.3);
			} else if (score == 10) {
				domain.setAttack(15.6);
			} else if (score == 11) {
				domain.setAttack(23);
			} else if (score == 12) {
				domain.setAttack(32.7);
			} else if (score >= 13) {
				domain.setAttack(43.1);
			}
			int standardscore = 0;
			if (age <= 39) {
				standardscore += 0;
			} else if (age >= 40 && age <= 44) {
				standardscore += 1;
			} else if (age >= 45 && age <= 49) {
				standardscore += 2;
			} else if (age >= 50 && age <= 54) {
				standardscore += 3;
			} else if (age >= 55 && age <= 59) {
				standardscore += 4;
			} else if (age >= 60 && age <= 64) {
				standardscore += 5;
			} else if (age >= 65 && age <= 79) {
				standardscore += 6;
			} else if (age >= 80 && age <= 84) {
				standardscore += 7;
			} else if (age >= 85) {
				standardscore += 8;
			}
			// 糖尿病
			if (dm == 1) {
				standardscore += 2;
			} else {
				standardscore += 0;
			}
			standardscore = standardscore - 2;
			if (standardscore == -2) {
				domain.setStandardattack(0.1);
			} else if (standardscore == -1) {
				domain.setStandardattack(0.2);
			} else if (standardscore == 0) {
				domain.setStandardattack(0.2);
			} else if (standardscore == 1) {
				domain.setStandardattack(0.3);
			} else if (standardscore == 2) {
				domain.setStandardattack(0.5);
			} else if (standardscore == 3) {
				domain.setStandardattack(0.8);
			} else if (standardscore == 4) {
				domain.setStandardattack(1.2);
			} else if (standardscore == 5) {
				domain.setStandardattack(1.8);
			} else if (standardscore == 6) {
				domain.setStandardattack(2.8);
			} else if (standardscore == 7) {
				domain.setStandardattack(4.4);
			} else if (standardscore == 8) {
				domain.setStandardattack(6.8);
			}
		}

		String str1 = "", str2 = "", str3 = "", str4 = "";
		if (knowledgeBase.getRiskGrade().equals("未患")) {

			str1 = "1、根据您目前健康状况，您未来10年缺血性心血管疾病的发病风险为";
			str2 = "2、只要将可以改变的危险因素降至参考值范围内，您未来10年缺血性心血管疾病的发病风险将降为";
			str3 = "3、您可以改善的危险因素有：";
			str4 = "4、为了您的健康，请从现在开始改变生活方式，以降低疾病的发病风险。";
		} else if (knowledgeBase.getRiskGrade().equals("已患")) {
			str1 = "1、您已患";
			str2 = "2、高危险因素会导致未来复发缺血性心血管疾病的风险增大；";
			str3 = "3、您可以改善的危险因素有：";
			str4 = "4、为了您的健康，避免再次复发缺血性心血管病事件，请您从现在开始改变生活方式。";

		}
		knowledgeBase.setKnowledgeBase1(str1);
		knowledgeBase.setKnowledgeBase2(str2);
		knowledgeBase.setKnowledgeBase3(str3);
		knowledgeBase.setKnowledgeBase4(str4);

		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();

		resultDto.put("knowledgeBaseResult", knowledgeBase);
		resultDto.put("hypertensionReport", domain);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	public double getStangdardAcctack(HypertensionReport hr, int year) {
		double bmi = hr.getBmi();

		int systolic = Integer.valueOf(hr.getSystolic());
		int diastolic = Integer.valueOf(hr.getDiastolic());
		int _systolic = systolic < 120 ? 100 : systolic;
		int _diastolic = diastolic < 80 ? 76 : diastolic;

		int sex = 0;
		if (hr.getSex() != null)
			sex = Integer.parseInt(hr.getSex());
		int age = hr.getAge();
		int fdisease = hr.getFdisease();

		if (bmi >= 22.5) {
			bmi = 22.5;
		}
		if (_systolic >= 110) {
			_systolic = 110;
		}
		if (_diastolic >= 70) {
			_diastolic = 70;
		}
		double ageDbp = age * _diastolic;

		int _fdisease = fdisease == 2 ? 0 : (fdisease + 1);
		double standardBeta = 22.949536 + (-0.156412 * age) + (-0.202933 * sex)
				+ (-0.033881 * bmi) + (-0.05933 * _systolic)
				+ (-0.128468 * _diastolic) + (-0.166121 * _fdisease)
				+ (0.001624 * ageDbp);
		double standardAttack = 1 - (Math
				.exp(-Math.exp((Math.log(year) - standardBeta) / 0.876925)));
		return standardAttack;
	}

	public String getfengxian(int score) {
		String fengxian = "";
		if (score >= 0 && score <= 4) {
			fengxian = "极低危风险";
		} else if (score >= 5 && score <= 9) {
			fengxian = "低微风险";
		} else if (score >= 10 && score <= 14) {
			fengxian = "中等风险";
		} else if (score >= 15 && score <= 19) {
			fengxian = "高风险";
		} else if (score >= 20) {
			fengxian = "极高风险";
		}
		return fengxian;
	}

	public String getMs(int i) {
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(1, "危险，代谢疾病来袭！@亲，你血糖有异常，健康红灯亮起啦！从现在开始，控制血糖，享受甜蜜蜜的人生哦。");
		map.put(2, "危险，代谢疾病来袭！@亲，你步入了高血压行列，听，警报拉响啦！珍爱生命，从现在开始，要控制血压哦。");
		map.put(3, "危险，代谢疾病来袭！@亲，你已经高血脂啦，血脂过多，容易造成“血稠”哦！清洁血管从现在开始，健康的血管，健康的生活！");
		map.put(4, "危险，代谢疾病来袭！@痛风（血尿酸高）找到咱家了呀！亲，会关节疼的哦！拒绝关节疼，降低血尿酸，从现在开始！");
		map.put(5, "危险，代谢疾病来袭！@亲，糖尿病合并高血压，两种慢性病啦！从现在开始，控制血糖、血压的同时，享受甜蜜蜜的人生！");
		map.put(6, "危险，代谢疾病来袭！@糖尿病合并高血脂，亲，两种慢性病啦！清洁血管的糖和血脂，从现在开始，健康的血管，健康的生活！");
		map.put(7, "危险，代谢疾病来袭！@糖尿病合并痛风（血尿酸高），亲，要引起注意啦！控制血糖、降低血尿酸，从现在开始！");
		map.put(8, "危险，代谢疾病来袭！@亲，你有高血压、高血脂两种慢性病，危险哦！控制血压，清洁血管的血脂，健康生活从现在开始！");
		map.put(9,
				"危险，代谢疾病来袭！@亲，两种慢性病高血压和痛风（血尿酸高）都找咱家啦！从现在开始，健康的生活，努力控制血压和血尿酸！");
		map.put(10, "危险，代谢疾病来袭！@亲，你有高血脂、痛风（血尿酸高）两种慢性病，危险哦！现在开始，控制血压的同时要控制血尿酸哦！");
		map.put(11, "危险，代谢疾病来袭！@亲，糖尿病、高血压、高血脂，三高啦！远离三高，拥有健康，从现在开始，控制血糖、血压和血脂！");
		map.put(12,
				"危险，代谢疾病来袭！@糖尿病、高血压、痛风/高血尿酸三种疾病，亲，让偶说啥好呢？赶紧的开始控糖、控压、控血尿酸，拒绝并发症，提高生活质量！");
		map.put(13,
				"危险，代谢疾病来袭！@亲啊，你有糖尿病、高血脂、痛风/高血尿酸，危险啊！控制血糖、血脂、血尿酸，现在势在必行啊，行动起来吧！偶看好你哦！");
		map.put(14,
				"危险，代谢疾病来袭！@亲，又是高血压、又是高血脂、又是尿酸高的，三种疾病！为了能以后的生活更美好，积极治疗疾病，预防并发症，从此刻开始！");
		map.put(15,
				"危险，代谢疾病来袭！@糖尿病、高血压、高血脂、痛风/高血尿酸，四种疾病同时有，亲，健康状况堪忧啊！从现在开始，积极控制疾病、预防并发症，迎接未来美好生活！");
		map.put(16,
				"小心，健康灰色状态！@亲，你目前的健康状态有些糟糕，请高度重视哦。请尽快调整生活方式，如有不适症状立即寻求医生的专业帮助。");
		map.put(17, "注意，健康受到威胁！@亲，你的健康状况勉勉强强。珍爱生命，及时调整生活方式，关注自己的健康！");
		map.put(18, "身体棒棒哒！@亲，您的健康状况良好，请继续保持哦！");
		return map.get(i).toString();
	}

	public String submitResulthealthRisks() {
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = new HypertensionReport();
		domain.setCustomerid(paramDto.getAsString("customerid"));
		domain.setHeight(paramDto.getAsString("height"));
		domain.setWeight(paramDto.getAsString("weight"));
		domain.setSex(paramDto.getAsString("sex"));
		domain.setDm(Integer.valueOf(paramDto.getAsString("tnb")));
		domain.setHypertension(Integer.valueOf(paramDto.getAsString("gxy")));
		domain.setGxz(Integer.valueOf(paramDto.getAsString("gxz")));
		domain.setTf(Integer.valueOf(paramDto.getAsString("tf")));
		String score = "";
		for (int i = 3; i <= 20; i++) {
			score = score + i + ":" + paramDto.getAsString(i + "") + ",";
		}
		domain.setScore(score.substring(0, score.length() - 1));
		domain.setEvalnumberid("5");
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = ff.format(new Date());
		domain.setSavedate(date);
		domain.setId(java.util.UUID.randomUUID().toString());
		String url = assessService.submitResulthealthRisks(domain);

		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		resultDto.put("url", "/evaluationReport/yjkreport/HealthReport.html");
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	public String resulthealthRisks() {
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		HypertensionReport domain = assessService.querybc(paramDto);

		String sex = domain.getSex(); // 性别

		int dm = domain.getDm(); // 糖尿病
		int hypertension = domain.getHypertension(); // 高血压
		int gxz = domain.getGxz(); // 高血脂
		int tf = domain.getTf(); // 痛风

		double bmi = Math.round(Double.parseDouble(domain.getWeight())
				/ Math.pow(Double.parseDouble(domain.getHeight()) / 100, 2)
				* 10) / 10.0;
		String scoreMap = domain.getScore();
		String scores[] = scoreMap.split(",");

		int score = 0; // 得分
		String miaoshu = ""; // 描述

		// 第2题任何一个选A，直接得分，不计算，并获取相应描述
		if (dm == 0 && hypertension != 0 && gxz != 0 && tf != 0) {
			score = 45;
			miaoshu = getMs(1);
		} else if (hypertension == 0 && dm != 0 && gxz != 0 && tf != 0) {
			score = 45;
			miaoshu = getMs(2);
		} else if (gxz == 0 && hypertension != 0 && dm != 0 && tf != 0) {
			score = 45;
			miaoshu = getMs(3);
		} else if (tf == 0 && hypertension != 0 && gxz != 0 && dm != 0) {
			score = 45;
			miaoshu = getMs(4);
		} else if (dm == 0 && hypertension == 0 && gxz != 0 && tf != 0) {
			score = 35;
			miaoshu = getMs(5);
		} else if (dm == 0 && gxz == 0 && hypertension != 0 && tf != 0) {
			score = 35;
			miaoshu = getMs(6);
		} else if (dm == 0 && tf == 0 && hypertension != 0 && gxz != 0) {
			score = 35;
			miaoshu = getMs(7);
		} else if (dm != 0 && tf != 0 && hypertension == 0 && gxz == 0) {
			score = 35;
			miaoshu = getMs(8);
		} else if (dm != 0 && gxz != 0 && hypertension == 0 && tf == 0) {
			score = 35;
			miaoshu = getMs(9);
		} else if (dm != 0 && hypertension != 0 && tf == 0 && gxz == 0) {
			score = 35;
			miaoshu = getMs(10);
		} else if (dm == 0 && hypertension == 0 && tf != 0 && gxz == 0) {
			score = 25;
			miaoshu = getMs(11);
		} else if (dm == 0 && hypertension == 0 && tf == 0 && gxz != 0) {
			score = 25;
			miaoshu = getMs(12);
		} else if (dm == 0 && hypertension != 0 && tf == 0 && gxz == 0) {
			score = 25;
			miaoshu = getMs(13);
		} else if (dm != 0 && hypertension == 0 && tf == 0 && gxz == 0) {
			score = 25;
			miaoshu = getMs(14);
		} else if (dm == 0 && hypertension == 0 && tf == 0 && gxz == 0) {
			score = 20;
			miaoshu = getMs(15);
		} else {
			// 只有第2题没有选A时，才会进入计分
			score = 0;

			// 第1题计分规则
			if (bmi >= 28) {
				score += 1;
			} else if (bmi >= 24 && bmi < 28) {
				score += 3;
			} else if (bmi >= 18.5 && bmi < 24) {
				score += 5;
			} else {
				score += 3;
			}

			// 第2题计分规则
			if (dm == 2 && hypertension == 2 && tf == 2 && gxz == 2) {
				score += 1;
			} else if (dm == 1 && hypertension == 1 && tf == 1 && gxz == 1) {
				score += 5;
			} else {
				score += 3;
			}

			// 第3-20题计分规则
			for (int i = 0; i < scores.length; i++) {
				if (scores[i].split(":")[1].equals("A")) {
					score += 1;
				} else if (scores[i].split(":")[1].equals("B")) {
					score += 3;
				} else {
					score += 5;
				}
			}

			if (score >= 20 && score <= 52) {
				miaoshu = getMs(16);
			} else if (score >= 53 && score <= 79) {
				miaoshu = getMs(17);
			} else if (score >= 80 && score <= 100) {
				miaoshu = getMs(18);
			}
		}

		List<HealthTag> jkjd = new ArrayList<HealthTag>(); // 健康标签
		String risks = ""; // 健康风险
		// 若健康评估标签为空，则得到标签列表；否则使用已有的标签列表
		if (domain.getTags() == null || "".equals(domain.getTags())) {
			HealthTag tagbmi = new HealthTag();
			if (bmi >= 28) {
				risks += "肥胖@";
				tagbmi.setHealthtag("肥胖");
				tagbmi.setHealthresult("你太胖了，已经严重影响到健康哦");
			} else if (bmi >= 24 && bmi < 28) {
				risks += "超重@";
				tagbmi.setHealthtag("超重");
				tagbmi.setHealthresult("超重是肥胖的前奏，离肥胖已经只有一步之遥了");
			} else if (bmi >= 18.5 && bmi < 24) {
				tagbmi.setHealthtag("身材匀称");
				tagbmi.setHealthresult("体重适宜，继续保持");
			} else {
				risks += "体重过低@";
				tagbmi.setHealthtag("骨感");
				tagbmi.setHealthresult("体重太轻，会对身体没有保护作用，适当要增加些肉肉的");
			}
			jkjd.add(tagbmi);

			Random rand = new Random();
			Map<String, Object> map = new HashMap<String, Object>();
			for (int value : getRandomMap().values()) {
				map.put("question", scores[value].split(":")[0]);
				map.put("options", scores[value].split(":")[1]);
				HealthTag tag = assessService.getbq(map);
				tag.setHealthtag(tag.getHealthtag().split("/")[rand.nextInt(2)]);
				jkjd.add(tag);
			}
			domain.setTags(turnTagListToString(jkjd));
			String result = assessService.submitResulthealthTags(domain);
			System.out.println("Tags save " + result + "!");
		} else {
			jkjd = turnStringToTagList(domain.getTags());
		}

		for (int i = 1; i <= 17; i++) {
			int q = Integer.parseInt(scores[i].split(":")[0]);
			if (q == 4) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "熬夜、睡眠不足@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "睡眠不规律@";
				}
			}
			if (q == 5) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "抽烟@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "抽烟@";
				}
			}
			if (q == 6) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "油烟吸入多@";
				}
			}
			if (q == 7) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "喝水少@";
				}
			}
			if (q == 8) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "饮酒过量@";
				}
			}
			if (q == 9) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "主食量偏大@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "不吃主食@";
				}
			}
			if (q == 10) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "不吃蔬菜、水果@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "蔬菜、水果摄入不足@";
				}
			}
			if (q == 11) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "肉类摄入过多@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "不吃荤类肉食@";
				}
			}
			if (q == 12) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "不吃海鲜产品@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "海鲜产品摄入过多@";
				}
			}
			if (q == 13) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "无运动@";
				}
				if (scores[i].split(":")[1].equals("B")) {
					risks += "运动不足@";
				}
			}
			if (q == 14) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "有缺钙倾向@";
				}
			}
			if (q == 15) {
				if (scores[i].split(":")[1].equals("A")) {
					risks += "肠胃功能紊乱@";
				}
			}
		}

		Dto resultDto = new BaseDto();
		resultDto.put("sex", sex);
		resultDto.put("score", score);
		if (risks.length() > 0) {
			resultDto.put("risks", risks.substring(0, risks.length() - 1));
		} else {
			resultDto.put("risks", "");
		}
		resultDto.put("miaoshu", miaoshu);
		resultDto.put("list", jkjd);

		ResponseDto responseDto = new ResponseDto();
		responseDto.pushResultDto(resultDto);

		super.setDataMap(responseDto);

		return SUCCESS;
	}

	public Map<Integer, Integer> getRandomMap() {

		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		Random rand = new Random();
		int x;
		for (int i = 0; map.size() < 10; i++) {
			x = rand.nextInt(18);
			if (!map.containsValue(x)) {
				map.put(i, x);
			}
		}
		return map;
	}

	public String turnTagListToString(List<HealthTag> jkjd) {
		String tags = "";
		for (HealthTag tag : jkjd) {
			String temp = tag.getHealthtagid() + "&";
			temp += tag.getQuestion() + "&";
			temp += tag.getOptions() + "&";
			temp += tag.getHealthtag() + "&";
			temp += tag.getHealthresult();
			tags += temp + "@";
		}
		tags = tags.substring(0, tags.length() - 1);
		return tags;
	}

	public List<HealthTag> turnStringToTagList(String tags) {
		List<HealthTag> tagList = new ArrayList<HealthTag>();
		String[] tagArray = tags.split("@");
		for (int i = 0; i < tagArray.length; i++) {
			String[] tagTemp = tagArray[i].split("&");
			HealthTag tag = new HealthTag();
			for(int n = 0;n < tagTemp.length; n++){
				tag.setHealthtagid(tagTemp[0]);
				tag.setQuestion(tagTemp[1]);
				tag.setOptions(tagTemp[2]);
				tag.setHealthtag(tagTemp[3]);
				tag.setHealthresult(tagTemp[4]);
			}
			tagList.add(tag);
		}
		return tagList;
	}

}
